/**
 * 树初始化
 */
var SchemaDic = {
    id: "schemaTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    schemaId: 0,
    schemaName: null
};

SchemaDic.onClickSource = function (e, treeId, treeNode) {
    SchemaDic.schemaId = treeNode.id;
    SchemaDic.schemaName = treeNode.name;
    //查询评估条目
    SchemaDic.search();
};

/**
 * 刷新当前选择节点的父节点
 */
SchemaDic.refreshParentNode = function () {
  SchemaDic.ztree.init();
}

/**
 * 查询评估条目列表
 */
SchemaDic.search = function () {
    var queryData = {};
    queryData['parentId'] = SchemaDic.schemaId;
    Dhcnlpschema.table.refresh({query: queryData, pageNumber: 1});
};


/**
 * 表格初始化
 */
var Dhcnlpschema = {
    id: "DhcNlpSchemaTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    schemaId: null,
    schemaName: null
};

/**
 * 初始化表格的列
 */
Dhcnlpschema.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {
            title: '代码',
            field: 'keyCode',
            width: 100,
            align: 'center',
            valign: 'middle'
        },
        {
            title: '名称',
            field: 'keyDesc',
            width: 100,
            align: 'center',
            valign: 'middle'
        },
        {
            title: '是否有效',
            field: 'isActive',
            width: 50,
            align: 'center',
            valign: 'middle',
            formatter:
                function (value, row, index) {
                    if (value == 1) {
                        return "<span class='label label-success'>是</span>";
                    } else {
                        return "<span class='label label-warning'>否</span>";
                    }
                }
        }]
};

/**
 * 点击评估条目，加载对应评估条目选项
 */
Dhcnlpschema.onDbClickDic = function (obj, $ele, field) {
    Dhcnlpschema.seItem = obj;
    Dhcnlpschema.schemaId = obj.id;
    Dhcnlpschema.schemaName = obj.name;
    Dhcnlpschema.search();
};

/**
 * 检查是否选中
 */
Dhcnlpschema.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Dhcnlpschema.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增评估条目
 */
Dhcnlpschema.add = function () {
    if (SchemaDic.schemaId) {
        var index = Sys.open('新增评估条目', Sys.backendPath + '/cdc/nlpschemadic/to_add', ['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#parentId").attr("value", SchemaDic.schemaId);
            $(iframeWin.document).find("#parentName").attr("value", SchemaDic.schemaName);
        });
    } else {
        Sys.info("请先选中某一父级标签！");
        return false;
    }
    this.layerIndex = index;
};

/**
 * 打开查看评估条目详情
 */
Dhcnlpschema.edit = function (id) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('标签详情', Sys.backendPath + '/cdc/nlpschemadic/to_update/' + id + "/", ['520px', '340px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#parentName").attr("value", SchemaDic.schemaName);
        });
        this.layerIndex = index;
    }
};


/**
 * 删除评估条目
 */
Dhcnlpschema.del = function (id, name) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name;
        flag = true;
    }
    if (flag) {
        var operation = function () {
            var ajax = new $ax(Sys.backendPath + "/cdc/nlpschemadic/delete", function (data) {
                Sys.success("删除成功!");
                Dhcnlpschema.table.refresh();
                SchemaDic.refreshParentNode();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("nlpSchemaDicId", id);
            ajax.start();
        };
        Sys.confirm("是否删除该标签及其子标签?", operation);
    }
};

$(function () {
    //查询数据源树
    var ztree = new $ZTree("schemaTree", Sys.backendPath + "/cdc/nlpschemadic/tree");
    ztree.bindOnClick(SchemaDic.onClickSource);
    ztree.init();
    SchemaDic.ztree = ztree;
    fuzzySearch('schemaTree', '#seachKey', null, true);
    //查询并初始化评估条目表格
    var itemColunms = Dhcnlpschema.initColumn();
    var itemtable = new BSTable(Dhcnlpschema.id, Sys.backendPath + "/cdc/nlpschemadic/list", itemColunms);
    itemtable.setPaginationType("server");
    itemtable.setShowExport(false);
    itemtable.setShowRefresh(false);
    itemtable.setShowToggle(false);
    itemtable.setShowColumns(false);
    itemtable.setOnDblClickRow(Dhcnlpschema.onDbClickDic);
    itemtable.setPageSize(12);
    itemtable.setQueryParams({"parentId": 0});
    Dhcnlpschema.table = itemtable.init();
});